package com.example.myspringboot.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.database.entity.StarRailCard;
import com.example.database.service.StarRailCardBizService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GachaService {

    @Resource
    private StarRailCardBizService starRailCardBizService;

    public List<Map<String, Object>> gachaTenTimes(List<Integer> up5, List<Integer> up4) {
        List<Map<String, Object>> gachaList = new ArrayList<>(10);
        int mustGet = 0;
        for (int i = 0; i < 10; i++) {
            Map<String, Object> map = new HashMap<>();
            int gacha = gacha();
            if (gacha == 3) {
                mustGet++;
                if (mustGet == 10) {
                    map.put("str", get4(up4) + "(⭐4)");
                    map.put("star", 4);
                    gachaList.add(map);
                } else {
                    map.put("str", getNormal3() + "(⭐3)");
                    map.put("star", 3);
                    gachaList.add(map);
                }
            } else if (gacha == 4) {
                map.put("str", get4(up4) + "(⭐4)");
                map.put("star", 4);
                gachaList.add(map);
            } else {
                map.put("str", get5(up5) + "(⭐5)");
                map.put("star", 5);
                gachaList.add(map);
            }
        }
        return gachaList;
    }

    private int gacha() {
        Random r = new Random();
        int i = r.nextInt(1000);
        if (i < 6) {
            return 5;
        }
        if (i < 57) {
            return 4;
        }
        return 3;
    }

    private String get4(List<Integer> up4) {
        Random r = new Random();
        int up = r.nextInt(2);
        if (up == 0) {
            return starRailCardBizService.getById(up4.get(r.nextInt(up4.size()))).getName();
        } else {
            int up2 = r.nextInt(2);
            if (up2 == 0) {
                return getNormal4Character(up4);
            } else {
                return getNormal4Weapon();
            }
        }
    }

    private String get5(List<Integer> up5) {
        Random r = new Random();
        int up = r.nextInt(2);
        if (up == 0) {
            return starRailCardBizService.getById(up5.get(r.nextInt(up5.size()))).getName();
        } else {
            return getNormal5();
        }
    }

    private String getNormal5() {
        QueryWrapper<StarRailCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 0)
                .eq("level", 5)
                .eq("rare", 0);
        List<StarRailCard> list = starRailCardBizService.list(queryWrapper);
        Random r = new Random();
        int i = r.nextInt(list.size());
        return list.get(i).getName();
    }

    private String getNormal4Character(List<Integer> up4) {
        QueryWrapper<StarRailCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 0)
                .eq("level", 4)
                .eq("rare", 0)
                .notIn("id", up4);
        List<StarRailCard> list = starRailCardBizService.list(queryWrapper);
        Random r = new Random();
        int i = r.nextInt(list.size());
        return list.get(i).getName();
    }

    private String getNormal4Weapon() {
        QueryWrapper<StarRailCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 1)
                .eq("level", 4)
                .eq("rare", 0);
        List<StarRailCard> list = starRailCardBizService.list(queryWrapper);
        Random r = new Random();
        int i = r.nextInt(list.size());
        return list.get(i).getName();
    }

    private String getNormal3() {
        QueryWrapper<StarRailCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 1)
                .eq("level", 3)
                .eq("rare", 0);
        List<StarRailCard> list = starRailCardBizService.list(queryWrapper);
        Random r = new Random();
        int i = r.nextInt(list.size());
        return list.get(i).getName();
    }
}
